<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.SQLException" %><%--
  Created by IntelliJ IDEA.
  User: 云哥
  Date: 2023-05-18
  Time: 10:21
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Hello JSP  Connection  MySQL By JDBC</h1>
<hr>
<%--jsp中的注释--%>
<%
    //小脚本
    Connection conn=null;
    try {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.建立连接
         conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/t161hospital?serverTimezone=UTC","root","root");
        //3.判断是否连接成功
        if(conn!=null){
            System.out.println("连接成功");
            out.write("数据库连接成功！<br/>");
        }else{
            System.out.println("数据库连接失败");
            out.print("数据库连接失败！！！<br/>");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        out.print("数据库连接失败！错误信息："+e.getMessage());
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally {
        if(conn!=null){
            conn.close();
            out.print("数据库连接已关闭！<br/>");
        }
    }
%>
<%--声明 方法--%>
<%!public String getName(){
    return "龙傲天";
}%>
<%--表达式--%>
<h1>欢迎<%=getName()%>进入页面</h1>
<hr>
<jsp:text>hello</jsp:text>
</body>
</html>
